/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-01-26 15:28:28
 * @LastEditors: your name
 * @LastEditTime: 2024-01-27 09:16:45
 */
const buildsFs = /*glsl*/ `
// Maximum number of cells a ripple can cross.
#define MAX_RADIUS 2

// Set to 1 to hash twice. Slower, but less patterns.
#define DOUBLE_HASH 0

// Hash functions shamefully stolen from:
// https://www.shadertoy.com/view/4djSRW
#define HASHSCALE1 .1031
#define HASHSCALE3 vec3(.1031, .1030, .0973)

float hash12(vec2 p)
{
	vec3 p3  = fract(vec3(p.xyx) * HASHSCALE1);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.x + p3.y) * p3.z);
}

vec2 hash22(vec2 p)
{
	vec3 p3 = fract(vec3(p.xyx) * HASHSCALE3);
    p3 += dot(p3, p3.yzx+19.19);
    return fract((p3.xx+p3.yz)*p3.zy);
}

void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
    // 计算法向量
    // 1.获取眼睛坐标当中的模型位置
    vec3 positionEC=fsInput.attributes.positionEC;
    // 2.使用偏导数计算法向量
    vec3 pos_dx = dFdx(positionEC);
    vec3 pos_dy = dFdy(positionEC);
    vec3 normalEC = normalize(cross(pos_dx, pos_dy));
    // 3.将眼睛坐标下的法向量，转为世界坐标czm_inverseViewRotation
    vec3 normalWC= normalize(czm_inverseViewRotation*normalEC);
    // // 计算模型在世界坐标下的位置czm_inverseView
    vec4 positionWC=normalize(czm_inverseView*vec4(positionEC,1.));
    // 计算当前片元坐标和当前面的法向量夹角
    float cosB=dot(positionWC.xyz,normalWC);

    vec2 uv = fsInput.attributes.texCoord_0;
    // 经过尝试，发现需要给uv乘上一个非常大的变量，才能满足效果，因为原版的circle太大了
    uv=uv*vec2(500.,500.);
    vec2 p0 = floor(uv);
    float iTime=czm_frameNumber/120.0;
    vec2 circles = vec2(0.);
    for (int j = -MAX_RADIUS; j <= MAX_RADIUS; ++j)
    {
        for (int i = -MAX_RADIUS; i <= MAX_RADIUS; ++i)
        {
			vec2 pi = p0 + vec2(i, j);
            #if DOUBLE_HASH
            vec2 hsh = hash22(pi);
            #else
            vec2 hsh = pi;
            #endif
            vec2 p = pi + hash22(hsh);

            float t = fract(0.3*iTime + hash12(hsh));
            vec2 v = p - uv;
            float d = length(v) - (float(MAX_RADIUS) + 1.)*t;

            float h = 1e-3;
            float d1 = d - h;
            float d2 = d + h;
            float p1 = sin(31.*d1) * smoothstep(-0.6, -0.3, d1) * smoothstep(0., -0.3, d1);
            float p2 = sin(31.*d2) * smoothstep(-0.6, -0.3, d2) * smoothstep(0., -0.3, d2);
            circles += 0.5 * normalize(v) * ((p2 - p1) / (2. * h) * (1. - t) * (1. - t));
        }
    }
    circles /= float((MAX_RADIUS*2+1)*(MAX_RADIUS*2+1));
    float intensity = mix(0.01, 0.15, smoothstep(0.1, 0.6, abs(fract(0.05*iTime + 0.5)*2.-1.)));
    vec3 n = vec3(circles, sqrt(1. - dot(circles, circles)));

    float ring=5.*pow(clamp(dot(n, normalize(vec3(1., 0.7, 0.5))), 0., 1.), 6.);
    // 判断当前渲染的片元是否在平面上
    float rateSnow=u_snowAlpha*smoothstep(0.7, 0.9,cosB);
    float rateRain=u_rainAlpha*smoothstep(0.7, 0.9,cosB);
    material.diffuse = mix(material.diffuse, vec3(ring) , rateRain);
    material.diffuse = mix(material.diffuse, vec3(1.0) , rateSnow);
}
`;

export default buildsFs;
